﻿'เชื่อมต่อฐานข้อมูล
Imports MySql.Data.MySqlClient
Imports System.IO
Imports System.Data

Public Class Manual
    Dim sql As MySqlConnection
    Dim idkey As String
    Dim ipconnect As String
    Dim usernamedb As String
    Dim dbname As String
    Dim mysqlpass As String
    Dim id_user As String
    Dim position_user As String
    Public Sub New(ByRef mysql_pass As String, ByRef ip_connect As String, ByRef user_namedb As String, ByRef db_name As String)
        InitializeComponent()
        mysqlpass = mysql_pass
        ipconnect = ip_connect
        usernamedb = user_namedb
        dbname = db_name
    End Sub

    Private Sub Manual_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sql = New MySqlConnection
        sql.ConnectionString = "server=" + ipconnect + ";user id=" + usernamedb + ";password=" + mysqlpass + ";database=" + dbname + ";Character set = utf8;"
        Try
            sql.Open()

        Catch ex As Exception

            MsgBox("Can't Connect to Database " + ex.Message)
            Me.Close()
        End Try
        Dim mysqlcommand As New MySqlCommand
        Dim mysqladaptor As New MySqlDataAdapter
        Dim mysqlreader As MySqlDataReader
        Dim count As String

        If sql.State = ConnectionState.Closed Then
            sql.Open()
        End If

        mysqlcommand.CommandText = "SELECT * FROM helpdesk order by idhelpdesk;"
        mysqlcommand.Connection = sql
        mysqladaptor.SelectCommand = mysqlcommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            ListViewhelp2.Items.Clear()

            While (mySqlReader.Read())

                With ListViewhelp2.Items.Add(mysqlreader("idhelpdesk"))
                    .SubItems.Add(mysqlreader("type_problem"))
                    .SubItems.Add(mysqlreader("agencies"))
                    .SubItems.Add(mysqlreader("state_problem"))
                    .subitems.add(mysqlreader("problem"))
                    .subitems.add(mysqlreader("date"))

                End With
            End While

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        sql.Close()

    End Sub

    Private Sub btnsavehelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsavehelp.Click
        Dim mysqlcommand As New MySqlCommand
        Dim mysqladeptor As New MySqlDataAdapter
        Dim mysqlreader As MySqlDataReader
        Dim count As String
        Dim respone As String


        If sql.State = ConnectionState.Closed Then
            sql.Open()
        End If
        'โค้ดบังคับกรอกข้อมูล

        If txtnameperson.Text = "" Or txtphoneperson.Text = "" Or txtproblem.Text = "" Or txtlocat.Text = "" Or txteditproblem.Text = "" Or ComboBoxAgen.Text = "" Or ComboBoxofficer.Text = "" Or ComboBoxstate.Text = "" Or ComboBoxtype.Text = "" Then

            MsgBox("ข้อความเตือน")
        Else
            Try
                mysqlcommand.CommandText = "INSERT INTO helpdesk (name_person,phone_person,agencies,type_problem,location,problem,officer,date,state_problem,edit_problem) VALUES ('" & txtnameperson.Text & "' , '" & txtphoneperson.Text & "', '" & ComboBoxAgen.Text & "', '" & ComboBoxtype.Text & "', '" & txtlocat.Text & "', '" & txteditproblem.Text & "', '" & ComboBoxofficer.Text & "', '" + Date.Now.ToString + "' , '" & ComboBoxstate.Text & "', '" & txteditproblem.Text & "' ) ; "
                mysqlcommand.CommandType = CommandType.Text
                mysqlcommand.Connection = sql
                mysqlcommand.ExecuteNonQuery()
                MsgBox("เก็บเข้าฐานข้อมูลแล้วจ้า!! by โตน ลูกพี่อารี")

                txtnameperson.Text = ""
                txtphoneperson.Text = ""
                ComboBoxAgen.Text = ""
                ComboBoxtype.Text = ""
                txtlocat.Text = ""
                txtproblem.Text = ""
                ComboBoxofficer.Text = ""
                ComboBoxstate.Text = ""
                txteditproblem.Text = ""


            Catch ex As Exception
                MsgBox(ex.ToString)
                sql.Close()
            End Try
        End If

        If sql.State = ConnectionState.Closed Then
            sql.Open()
        End If

        mysqlcommand.CommandText = "SELECT * FROM helpdesk order by idhelpdesk ;"
        mysqlcommand.Connection = sql
        mysqladeptor.SelectCommand = mysqlcommand

        Try
            mysqlreader = mysqlcommand.ExecuteReader
            ListViewhelp2.Items.Clear()
            While (mysqlreader.Read())
                With ListViewhelp2.Items.Add(mysqlreader("idhelpdesk"))
                    .subitems.add(mysqlreader("type_problem"))
                    .subitems.add(mysqlreader("agencies"))
                    .subitems.add(mysqlreader("state_problem"))
                    .subitems.add(mysqlreader("problem"))
                    .subitems.add(mysqlreader("date"))
                End With
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        sql.Close()


    End Sub
    

    

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
        Dim NextForm As edithelpdesk = New edithelpdesk(mysqlpass, ipconnect, usernamedb, dbname)
        NextForm.Show()
        Me.Hide()
    End Sub
End Class